package client;

import java.util.ArrayList;

import datastructure.Command;
import datastructure.PatientRecord;
/**
 * A class which converts strings to patient records.
 * @author dt08sj7
 *
 */
public class InformationHandler {
	
/**
 * Converts a string to an ArrayList with patienrecords. 
 * @param text
 * 			The rext string to be converted. It must follow the Johansson-Hallberg protocol.
 * @return
 * 			An ArrayList<PatientRecord> with the patient records decoded from text.
 */			
	public static ArrayList<PatientRecord> parseToRecords(String text) {
		ArrayList<PatientRecord> records = new ArrayList<PatientRecord>();
		String[] recordChunks = text.split(";");
		for (int i = 0; i < recordChunks.length; i++) {
			String[] bit = recordChunks[i].split(":");
			System.out.println("Innan replace: " + bit[7]);
			records.add(new PatientRecord(bit[0], bit[1], bit[2], bit[3],
					bit[4], bit[5], Integer.parseInt((bit[6])), bit[7].replace(
							'.', ':')));
			System.out.println("Efter replace: " + bit[7]);

		}
		return records;
	}

/**
 * Converts a Command to a string.
 * @param cmd
 * 			The command to be converted.
 * @return
 * 			Returns the string of the command.
 */

	public static String commandToText(Command cmd) {
		return cmd.toString();
	}
}
